// 导入 antd 组件库
import { Card, Form, Input, Button, Checkbox, message } from 'antd';
import { useDispatch, useSelector } from 'react-redux';

// 导入logo图片
import logo from '@/assets/logo.png';

// 导入组件样式
import styles from './Login.module.scss';

// 导入action
import { login } from '@/store/actions/login';
import { useHistory } from 'react-router-dom';

export const Login = () => {
  const dispatch = useDispatch();
  const history = useHistory()

  // 表单校验通过
  const onFinish = async (values) => {
    try {
      await dispatch(login(values.mobile, values.code))
      history.replace(
        history.location.state ? history.location.state.from : '/home'
      );
    } catch (error) {
      if (!error.response) {
        message.warn('网络新闻')
        return
      }
      if (error.response && error.response.status === 400) {
        message.warn(error.response.data.message);
      }
    }
  };

  // 表单校验失败
  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };
  return (
    <div className={styles.root}>
      {/* 登录card容器 */}
      <Card
        className="geek-card"
        bordered={false}
        bodyStyle={{
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
        }}
      >
        {/* logo */}
        <img className="login-logo" src={logo} />
        {/* 表单 */}
        <Form
          validateTrigger={['onBlur', 'onChange']} // 统一制定表单元素验证触发时机
          initialValues={{
            mobile: '13911111111', // 测试时方便
            code: '246810',
            remember: true, // 表单初始值设置
          }}
          onFinish={onFinish} // 表单提交验证通过回调
          onFinishFailed={onFinishFailed} // 表单验证失败回调
          autoComplete="off" // 是否打开表单的输入记忆功能
        >
          <Form.Item
            name="mobile"
            rules={[
              {
                pattern: /^1[3-9]\d{9}$/,
                message: '手机号码格式不对',
              },
              {
                required: true,
                message: '请输入手机号',
              },
            ]}
          >
            <Input size="large" placeholder="请输入手机号" />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              {
                len: 6,
                message: '验证码6位',
              },
              {
                required: true,
                message: '请输入验证码',
              },
            ]}
          >
            <Input size="large" placeholder="请输入验证码" />
          </Form.Item>

          <Form.Item name="remember" valuePropName="checked">
            <Checkbox style={{ color: '#1890ff' }}>
              我已阅读并同意「用户协议」和「隐私条款」
            </Checkbox>
          </Form.Item>

          <Form.Item>
            <Button
              size="large"
              type="primary"
              htmlType="submit"
              style={{ width: '100%' }}
            >
              登 录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  );
};
